#pragma once
#include <iostream>
#include <poll.h>
#include "Socket.hpp"
#include "log.hpp"

using namespace std;
static const uint16_t defaultport = 8080;
static const int fd_num_max = 64;//这个是可以修改的，不像select是固定的
int defaultfd = -1;
int non_event = 0;

class PollServer
{
public:
    PollServer(const uint16_t port = defaultport) : _port(port)
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            _event_fds[i].fd = -1;
            _event_fds[i].events = non_event;
            _event_fds[i].revents = non_event;
        }
    }
    void Init()
    {
        _listensock.Socket();
        _listensock.Bind(_port);
        _listensock.Listen();
    }
    void Accepter()
    {
        uint16_t clientport;
        string clientip;
        int fd = _listensock.Accept(&clientport, &clientip);
        if (fd < 0)
            return;
        int i = 0;
        for (; i < fd_num_max; i++)
        {
            if (_event_fds[i].fd!= defaultfd)
                continue;
            else
                break;
        }
        if (i == fd_num_max) // 数组满了，poll fd满了
        {
            log(Warning, "server is full, close %d now!", fd);
            close(fd);
            //可以进行扩容
        }
        else
        {
            _event_fds[i].fd = fd;
            _event_fds[i].events=POLLIN;
            _event_fds[i].revents = non_event;
            PrintFd();
        }
    }
    void Recver(int fd, int pos)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer) - 1); // 有bug，因为应用层没有定制协议，不一定能读到完整的报文，我们这里不定制先
        if (n > 0)
        {
            buffer[n] = 0;
            cout << "get a messge: " << buffer << endl;
        }
        else if (n == 0)
        {
            log(Info, "client quit, me too, close fd is : %d", fd);
            close(fd);
            _event_fds[pos].fd= defaultfd;
        }
        else
        {
            log(Warning, "recv error: fd is : %d", fd);
            close(fd);
            _event_fds[pos].fd = defaultfd;
        }
    }
    void Dispatcher()
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            int fd = _event_fds[i].fd;
            if (fd == defaultfd)
                continue;
            if (_event_fds[i].revents&POLLIN)
            {
                // 就绪的文件描述符
                if (fd == _listensock.GetFd()) // 如果是listen的fd，就接收连接，添加到数组中
                {
                    Accepter();
                }
                else // 进行网络通信的fd了
                {
                    Recver(fd, i);
                }
            }
        }
    }
    void Start()
    {
        //先把listensock添加到数组中
        _event_fds[0].fd = _listensock.GetFd();
        _event_fds[0].events = POLLIN;
        int timeout=2000;
        while (1)
        {
            // accept?不能直接accept！检测并获取listensock上面的事件，新连接到来，等价于读事件就绪
            // 这里不能直接accept，因为可能还没有新的连接
            int n = poll(_event_fds, fd_num_max, timeout);
            if (n < 0)
            {
                cerr << "poll error" << endl;
            }
            else if (n == 0)
            {
                cout << "time out, timeout: " <<timeout << endl;
            }
            else
            {
                // 有事件就绪了
                cout << "get a new link!!!!!" << endl;
                Dispatcher(); // 给就绪的fd分配任务
            }
        }
    }
    void PrintFd()
    {
        cout << "online fd list: ";
        for (int i = 0; i < fd_num_max; i++)
        {
            if (_event_fds[i].fd== defaultfd)
                continue;
            cout << _event_fds[i].fd << " ";
        }
        cout << endl;
    }
    ~PollServer()
    {
    }

private:
    Sock _listensock;
    uint16_t _port;
    struct pollfd _event_fds[fd_num_max]; // 数组, 用户维护的！
};
